define(["app", "datetimepickerCn"], function(app) {
	
	// 日期时间选择器打开之后首先显示的视图。 可接受的值：
	//
	// 0 or 'hour' for the hour view
	// 1 or 'day' for the day view
	// 2 or 'month' for month view (the default)
	// 3 or 'year' for the 12-month overview
	// 4 or 'decade' for the 10-year overview. Useful for date-of-birth
	// datetimepickers.
	function getSuitableView(dateFormat) {
		if (dateFormat == null || dateFormat.length == 0
				|| dateFormat == "yyyy-mm-dd") {
			return {
				minView : 2
			};
		} else if (dateFormat == "yyyy-mm") {
			return {
				startView : 3,
				minView : 3
			};
		} else if (dateFormat == "yyyy-mm-dd hh") {
			return {
				minView : 1
			};
		} else if (dateFormat == "yyyy-mm-dd hh:ii") {//yyyy-mm-dd hh:mm
			return {
				minView : 0
			};
		} else if (dateFormat == "yyyymmdd") {
			return {
				minView : 2
			};
		}
		return {};
	}
	
	function parseDate(str){   
	   if(typeof str == 'string'){   
	     var results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) *$/);
	     if(results && results.length>3){
	     return  new Date(parseInt(results[1],10),(parseInt(results[2],10) -1),parseInt(results[3],10));    
	       }
	     str+=":00";
	     results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2}) *$/);   
	     if(results && results.length>6)   
	       return new Date(parseInt(results[1],10),parseInt(results[2],10) -1,parseInt(results[3],10),parseInt(results[4],10),parseInt(results[5],10),parseInt(results[6],10));    
	     results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{1,9}) *$/);   
	     if(results && results.length>7)   
	       return new Date(parseInt(results[1],10),parseInt(results[2],10) -1,parseInt(results[3],10),parseInt(results[4],10),parseInt(results[5],10),parseInt(results[6],10),parseInt(results[7],10));    
	   }   
	   return null;   
	}
	function parseDateWithOutLine(str){   
		   if(typeof str == 'string'){   
		     var results = str.match(/^ *(\d{4})(\d{1,2})(\d{1,2}) *$/);
		     if(results && results.length>3){
		     return  new Date(parseInt(results[1],10),(parseInt(results[2],10) -1),parseInt(results[3],10));    
		       }
		     results = str.match(/^ *(\d{4})(\d{1,2})(\d{1,2}) +(\d{1,2})(\d{1,2})(\d{1,2}) *$/);   
		     if(results && results.length>6)   
		       return new Date(parseInt(results[1],10),parseInt(results[2],10) -1,parseInt(results[3],10),parseInt(results[4],10),parseInt(results[5],10),parseInt(results[6],10));    
		     results = str.match(/^ *(\d{4})(\d{1,2})(\d{1,2}) +(\d{1,2})(\d{1,2})(\d{1,2})\.(\d{1,9}) *$/);   
		     if(results && results.length>7)   
		       return new Date(parseInt(results[1],10),parseInt(results[2],10) -1,parseInt(results[3],10),parseInt(results[4],10),parseInt(results[5],10),parseInt(results[6],10),parseInt(results[7],10));    
		   }   
		   return null;   
		}

	app.lazy.directive("jssDatetimepicker", function() {
		return {
			restrict : "A",
			require: "?ngModel",
			// added by xuyunzhou, callback while pick a date
			scope : {
				onSelect : "&onSelect",
				date : "=ngModel"
			},
			
			
			link : function(scope, elem, attrs, ngModel) {
				if(!ngModel) {
					return;
				}
				
				var dateFormat = attrs["jssDatetimepicker"] || attrs["dateFormat"];
				var view = getSuitableView(dateFormat);

				var dpglobal = $.fn.datetimepicker.DPGlobal;
				var format = dpglobal.parseFormat(dateFormat || "yyyy-mm-dd", "standard");
				/**
				 * position位置值：bottom-right，bottom-left,top-right,top-left
				 */
                var position = attrs.pickerPosition;
				$(elem).datetimepicker($.extend({
					format : dateFormat || "yyyy-mm-dd",
					language : 'zh-CN',
					todayBtn : true,
					autoclose : true,
					pickerPosition: position,
					forceParse : 0
				}, view))
				.on('changeDate', function(ev){
					var date = dpglobal.formatDate(ev.date, format, "zh-CN", "standard");
				    // scope.$eval(attrs["ngModel"] + "='" + date+"'");
					// can use this directly.
//				    ngModel.$setViewValue(date);
					scope.date = date;
				    if(scope.onSelect) {
				    	scope.onSelect({$date:date});
				    }
				    scope.$apply();
				});
				scope.$watch("date", function(){
					if(scope.date){
						var date = new Date();
					    if(typeof scope.date == 'string'){
					    	if(scope.date.indexOf("-")>0){  //2012-12-25
								date = parseDate(scope.date);
							}else{//20121225 lf增加at 2014-9-24
								date = parseDateWithOutLine(scope.date);
							}
					    }else{
							date.setTime(scope.date);  //13422232323232
						}
						
						$(elem).datetimepicker("setDate",date);
					}
				});
				
				
				
			}
			
			
			
			
		};
	});
	
});